package com.ashes.telbots.service.impl;

import com.ashes.telbots.utils.TelbotsUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

/**
 * @author chengxuhui
 * @description 描述
 */
@Data
@Slf4j
@Service
@Lazy
public class TelbotsServiceImpl extends TelegramLongPollingBot {


    private String botname = "hanxiaosir_bot";
    private String token = "6860829160:AAHOfrVeuXB6LkVD3bTPJ46h9ayr-J7lTJg";

    @Bean
    public DefaultBotOptions getDefaultBotOptions() {
        return new DefaultBotOptions();
    }


    public TelbotsServiceImpl(DefaultBotOptions defaultBotOptions) {
        super(defaultBotOptions);
    }

    @Override
    public String getBotUsername() {
        return botname;
    }

    @Override
    public String getBotToken() {
        return token;
    }

    @Override
    public void onUpdateReceived(Update update) {
        Message msg = update.getMessage();
        log.info("收集到的信息,{}", msg);
        SendMessage message = null;

        if (("/take").equalsIgnoreCase(msg.getText())) {
            message = TelbotsUtils.getAutoInviteGroup(msg);
        } else if (("/weather").equalsIgnoreCase(msg.getText())) {
            message = TelbotsUtils.getWeatherInfo(msg);
        } else {
            message = TelbotsUtils.getAutoReply(msg);
        }

        try {
            execute(message);
        } catch (TelegramApiException e) {
            log.info("报错信息,{}", e.getMessage());
            e.printStackTrace();
        }
    }
}
